﻿Imports System.Net.Mail
Imports AUtility

Partial Public Class Prijava
    Inherits System.Web.UI.Page

    Dim iRow As TLDS.PrijavaRow
    Dim iTable As TLDS.PrijavaDataTable = New TLDS.PrijavaDataTable()
    Dim selectStr As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub Spremi()

        iRow = iTable.NewPrijavaRow()

        iRow.ImePrezime = String.Format("{0} {1}", txtIme.Text, txtPrezime.Text)
        iRow.Email = txtEmail.Text
        iRow.Mobitel = txtMobitel.Text
        iRow.Adresa = txtAdresa.Text
        iRow.Saznali = txtSaznali.Text
        iRow.Komentar = txtKomentar.Text
        'iRow.Slika = txtPicture.Text

        iTable.AddPrijavaRow(iRow)

        Dim iTA As TLDSTableAdapters.PrijavaTableAdapter = New TLDSTableAdapters.PrijavaTableAdapter()
        iTA.Update(iTable)

        lblMessage.Visible = True
        lblMessage.Text = "Uspješna prijava, javit ćemo vam se uskoro!"
        prijavaDiv.Visible = False

        Try
            SendMail()
        Catch ex As Exception
            lblMessage.Visible = True
            lblMessage.Text = ex.Message
        End Try

    End Sub

    Private Sub SendMail()
        Dim mm As New MailMessage()
        Dim from As New MailAddress(txtEmail.Text)

        mm.From = from
        'plus
        'mm.From = New MailAddress("teniszg@gmail.com")
        mm.To.Add("danko.greiner@gmail.com")
        mm.To.Add("danko.greiner.sms@gmail.com")

        mm.Subject = String.Format("Prijava novog igrača {0} {1}", txtIme.Text, txtPrezime.Text)
        mm.Body = String.Format("{0} {1}{2}{3}{4}{5}{6}{7}{8}{9}{10}", txtIme.Text, txtPrezime.Text, Environment.NewLine, txtEmail.Text, Environment.NewLine, txtMobitel.Text, Environment.NewLine, txtSaznali.Text, Environment.NewLine, txtKomentar.Text + Environment.NewLine, DateTime.Now)

        'hosting centar
        'Dim smpt As New SmtpClient("217.14.208.72")
        Dim smpt As New SmtpClient("89.201.164.205")
        smpt.Credentials = New System.Net.NetworkCredential("danko@danko.hr", "danko123")

        'plus
        'Dim smpt As New SmtpClient("89.201.164.205")
        'Dim SMTPUserInfo As System.Net.NetworkCredential = New System.Net.NetworkCredential("test123", "test123")
        'smpt.UseDefaultCredentials = False
        'smpt.Credentials = SMTPUserInfo

        smpt.Send(mm)

        Try
            DodajIgraca()

        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnSpremi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpremi.Click
        Try
            If (chkPrivacam.Checked) Then
                Spremi()
            Else
                lblMessage.Text = "Morate prihvatiti pravila! :)"
                lblMessage.Visible = True
            End If
        Catch ex As Exception
            lblMessage.Visible = True
            lblMessage.Text = "Pogreška prilikom prijave!<br> Prijavite se na mail: teniszg@gmail.com"

            lblMessage.Text += "<br>" + ex.Message
        End Try
    End Sub

    Private Sub DodajIgraca()
        Dim iTA As TLDSTableAdapters.IgracTableAdapter = New TLDSTableAdapters.IgracTableAdapter()
        Dim iDT As New TLDS.IgracDataTable()

        Dim iRow As TLDS.IgracRow
        iRow = iDT.NewIgracRow()
        iRow.Ime = txtIme.Text
        iRow.Prezime = txtPrezime.Text
        iRow.Mobitel = txtMobitel.Text
        iRow.Mail = txtEmail.Text
        iRow.Picture = "http://tenisligaplayers.tk-osmica.hr/Pages/Blog/facebook.jpg"
        iRow.Aktivan = True
        iRow.Password = String.Format("{0}{1}{2}{3}", txtPrezime.Text.Substring(1, 1), txtPrezime.Text.Substring(txtPrezime.Text.Length - 2, 1), txtIme.Text.Substring(1, 1), txtIme.Text.Substring(txtIme.Text.Length - 2, 1))
        'iRow("Spol") = "M"


        iDT.Rows.Add(iRow)

        iTA.Update(iDT)
    End Sub

End Class